package controller;

import com.alibaba.fastjson.JSON;
import com.zuche.framework.tradingPlatform.*;
import controller.remote.CartpRemoteClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by PCG on 2016/4/17.
 */
@Controller
@RequestMapping(value = "fcar/",produces = "text/html;charset=utf-8")
public class YeepayController {
    private static final Logger logger = LoggerFactory.getLogger(YeepayController.class);


//        paramMap.put(RequestParams.PAYER_TEL, "18202529195");
//        paramMap.put(RequestParams.IDENTITY_ID, "120110198608170318");
//        paramMap.put(RequestParams.PAYER_NAME, "郭艳松");
//    6228270021221208879  农行
//    6217900200002631811  中国银行


    /**
     * 易宝支付EPOS：带卡信息支付，可以指定是否绑卡，如果绑卡，后续交易可以使用绑卡支付
     *
     * 此交易 易宝会向客户发送确认短信，需要客户确认才能完成
     * @param request
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/testYeepayCardPay")
    @ResponseBody
    public Object testYeepayCardPay(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.GROUP_TYPE, "yeepayCardPay");
        paramMap.put(RequestParams.PAYER_NAME,"荣雪梅");
        paramMap.put(RequestParams.SOURCE_ID,"CCB");
        paramMap.put(RequestParams.CARD_NO, "6217000010080210654");
        paramMap.put(RequestParams.PAYER_TEL,"13691275856");
        paramMap.put(RequestParams.IDENTITY_TYPE,"0");
        paramMap.put(RequestParams.IDENTITY_ID,"131082198602210525");
        paramMap.put(RequestParams.CARD_TYPE, "2");
        paramMap.put(RequestParams.BUSINESS_MONEY, "1");
        paramMap.put(RequestParams.BUSINESS_ID, System.currentTimeMillis() + "");
        paramMap.put(RequestParams.BUSINESS_NAME, "神州闪贷自动托收银行卡余额还款");
        paramMap.put(RequestParams.SYS_GROUP,"7");
        paramMap.put(RequestParams.IS_VALIDATE, "true");
        paramMap.put(RequestParams.PAYER_ID, "3219");
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://127.0.0.1:8081/test/testCartp/testMvcModeReturn.do_");
        logger.error(" [carttp调用参数] : " + JSON.toJSONString(paramMap));
        Map<String,String> resultMap = CartpRemoteClient.doRpcInvoke(paramMap, true);
        logger.error(" [carttp返回结果] : " + JSON.toJSONString(resultMap));
        return resultMap;
    }

    @RequestMapping(value = "/testYeepayBindPay")
    @ResponseBody
    public byte[] testYeepayBindPay(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.GROUP_TYPE, "yeepayBindPay");
        paramMap.put(RequestParams.BUSINESS_MONEY, "0.1");
        paramMap.put(RequestParams.BUSINESS_ID, System.currentTimeMillis() + "");
        paramMap.put(RequestParams.BUSINESS_NAME, "test123");
        paramMap.put("tp_yeepayBindId", "1");
        paramMap.put(RequestParams.PAYER_ID, "aaa1");
        paramMap.put(RequestParams.SYS_GROUP, "5");
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://127.0.0.1:8081/test/testCartp/testMvcModeReturn.do_");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);
        writeRpc(response, result);
        return null;
    }

    @RequestMapping(value = "/testYeepayBindQuery")
    @ResponseBody
    public byte[] testYeepayBindQuery(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.GROUP_TYPE, "yeepayBindQuery");
        paramMap.put(RequestParams.PAYER_ID, "aaa1");
        paramMap.put(RequestParams.SYS_GROUP, "5");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);
        writeRpc(response, result);
        return null;
    }

    @RequestMapping(value = "/testYeepayUnBind")
    @ResponseBody
    public byte[] testYeepayUnBind(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.GROUP_TYPE, "yeepayUnbind");
        paramMap.put(RequestParams.PAYER_ID, "aaa1");
        paramMap.put(RequestParams.SYS_GROUP, "5");
        paramMap.put("tp_yeepayBindId", "1");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);
        writeRpc(response, result);
        return null;
    }

    /**
     * 易宝预授权
     * @param request
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/testYeepayAuth")
    @ResponseBody
    public Object testYeepayAuth(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.GROUP_TYPE, "yeepayAuth");
        //==================================
        //传递绑卡id，以下信息不用传递
        paramMap.put(RequestParams.PAYER_NAME, request.getParameter("name"));
        paramMap.put(RequestParams.CARD_NO, request.getParameter("cardNo"));
        paramMap.put(RequestParams.PAYER_TEL, request.getParameter("mobile"));
        paramMap.put(RequestParams.IDENTITY_TYPE, IdentityType.getTypeCode(IdentityType.ID_CARD).toString());
        paramMap.put(RequestParams.IDENTITY_ID, request.getParameter("sfz"));
        //==================================
        //绑定id
        paramMap.put(RequestParams.BIND_ID, request.getParameter("bindId"));

        paramMap.put(RequestParams.BUSINESS_MONEY, "3.87");
        paramMap.put(RequestParams.BUSINESS_ID, System.currentTimeMillis() + "");
        paramMap.put(RequestParams.BUSINESS_NAME, "test1234");
        paramMap.put(RequestParams.SYS_GROUP, request.getParameter("sysGroup"));
        paramMap.put(RequestParams.PAYER_ID, "5125392");
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://127.0.0.1:8081/test/testCartp/testMvcModeReturn.do_");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);
        return result;
    }


    private void writeRpc(HttpServletResponse response, Map<String, String> paramMap) {
        Map<String, String> result = TradingServiceExecutor.doRpc(paramMap,
                true);
        try {
            response.setContentType("text/html;charset=UTF-8");
            StringBuilder sb = new StringBuilder("");
            for (Field f : ReplyRequestParams.class.getDeclaredFields()) {
                if (result.containsKey(f.get(null).toString())) {
                    sb.append(f.getName()).append(" = ")
                            .append(result.get(f.get(null).toString()))
                            .append("<br/>");
                }
            }
            for (String key : result.keySet()) {
                if (key.startsWith(RequestParams.CUSTOMIZED_FLAG)) {
                    sb.append(key).append(" = ").append(result.get(key));
                }
            }
            response.getWriter().write(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

